package com.example.apitest;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.parse.Parse;
import com.parse.ParseAnalytics;
import com.parse.PushService;


import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.util.Log;
public class MainActivity extends Activity {
	private static final String TAG = "MainActivity";	
	private ParseAPI	parseapi = new ParseAPI();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	
		super.onCreate(savedInstanceState);
		

//		Parse.initialize(this, "xRWeO2RARaah3JsmOgFHF5YLj1dCuEthujG6w0h5", "fAdtJ4gwGObPLitsXGobU7ghpplsZh0NunoBIYrh"); 
//		PushService.setDefaultPushCallback(this, MainActivity.class);
//		ParseAnalytics.trackAppOpened(getIntent());
		parseapi.initialize(this, getIntent());
		parseapi.Subscribe(this, "adbd", MainActivity.class);
	
		
		Log.e(TAG,"appstart");

		setContentView(R.layout.activity_main);
		
        Button btnExit=(Button)findViewById(R.id.button1);  

        btnExit.setOnClickListener(new OnClickListener() {  
            @Override  
            public void onClick(View v) {  
//                finish();
            	Log.i(TAG,"onClick---start");
                MyThread myThread1 = new MyThread();   
                myThread1.start(57952262);
                Log.i(TAG,"onClick---end");
       	
            }  
        }); 		
	}

    private static String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
    
    class MyThread extends Thread {
    	private int m_Userid = 0;
    	
		public void start(int userid) {
			m_Userid = userid;
			start();
		}

		public void run() {
			DouBanAPI api = new DouBanAPI();
			JSONObject	object = null;
			int Userid = m_Userid;
			object = api.SyncAccountBook(Userid);
			
			BaiHuiAPI baihuiapi = new BaiHuiAPI();
			Log.i(TAG,"run---start");
			try {
				if(false){
					JSONArray jsonArray = object.getJSONArray("collections");
			        for (int i = 0; i < jsonArray.length(); i++) {
						BookObject bookobject = new BookObject(jsonArray.getJSONObject(i).getJSONObject("book"));
						baihuiapi.Add(bookobject);
			        }					
				}else if(false){
					baihuiapi.Update(new BookObject());
				}else if(false){
					baihuiapi.View(new BookObject());
				}else if(false){
					baihuiapi.Delete(new BookObject());
				}else if(true){
					JSONObject user = new JSONObject();
					user.put(ParseAPI.PARSE_USERNAME, "yang1");
					user.put(ParseAPI.PARSE_PASSWORD, "yangP");
					user.put(ParseAPI.PARSE_EMAIL, "yang@madil.com");
					parseapi.GetCurrentUser(user);
					parseapi.SignUp(user);
					parseapi.GetCurrentUser(user);
					parseapi.LogOut();
					parseapi.GetCurrentUser(user);
					parseapi.LogIn(user);
					parseapi.GetCurrentUser(user);
				}

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			Log.i(TAG,"run---end");
		}
    }  
    
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
